﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Sunny.Controls
{
	/// <summary>
	/// Toolbar.xaml 的交互逻辑
	/// </summary>
	public partial class Toolbar : Grid
	{
		public System.Windows.Controls.UIElementCollection Items
		{
			get
			{
				return this.ItemContainer.Children;
			}
		}

		private Searcher.SimpleSearcher _Searcher;
		public Searcher.SimpleSearcher Searcher
		{
			get
			{
				if (this._Searcher == null)
					this._Searcher = new Searcher.SimpleSearcher(this.SearcherContainer.Children);
				return this._Searcher;
			}
		}

		public System.Windows.Controls.UIElementCollection Buttons
		{
			get
			{
				return this.ButtonContainer.Children;
			}
		}

		public static readonly RoutedEvent QueryEvent = EventManager.RegisterRoutedEvent("Query", RoutingStrategy.Bubble, typeof(System.EventHandler<QueryEventArgs>), typeof(Toolbar));
		public event System.EventHandler<QueryEventArgs> Query
		{
			add { AddHandler(QueryEvent, value); }
			remove { RemoveHandler(QueryEvent, value); }
		}
		protected void RaiseQueryEvent(QueryEventArgs args)
		{
			args.RoutedEvent = QueryEvent;
			this.RaiseEvent(args);
		}

		public static readonly RoutedEvent ReloadEvent = EventManager.RegisterRoutedEvent("Reload", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Toolbar));
		public event RoutedEventHandler Reload
		{
			add { AddHandler(ReloadEvent, value); }
			remove { RemoveHandler(ReloadEvent, value); }
		}
		protected void RaiseReloadEvent()
		{
			this.RaiseEvent(new RoutedEventArgs { RoutedEvent = ReloadEvent });
		}

		public static readonly RoutedEvent ExportEvent = EventManager.RegisterRoutedEvent("Export", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Toolbar));
		public event RoutedEventHandler Export
		{
			add { AddHandler(ExportEvent, value); }
			remove { RemoveHandler(ExportEvent, value); }
		}
		protected void RaiseExportEvent()
		{
			this.RaiseEvent(new RoutedEventArgs { RoutedEvent = ExportEvent });
		}

		//public event System.EventHandler<QueryEventArgs> Query;
		//protected virtual void OnQuery(QueryEventArgs args)
		//{
		//	if (this.Query != null)
		//		this.Query(this, args);
		//}

		public event System.EventHandler Clear;
		protected virtual void OnClear()
		{
			foreach (SearchItem item in this.Searcher)
				item/*.SimpleSearcherLayout*/.Empty();

			if (this.Clear != null)
				this.Clear(this, System.EventArgs.Empty);

			this.RaiseQueryEvent(new QueryEventArgs(null, null));
		}

		private Sunny.Controls.Button _SimpleSearchButton;
		public Sunny.Controls.Button SimpleSearchButton
		{
			get
			{
				if (this._SimpleSearchButton == null)
				{
					this._SimpleSearchButton = new Sunny.Controls.Button();
					this._SimpleSearchButton.Icon = (System.Windows.Media.ImageSource)new System.Windows.Media.ImageSourceConverter().ConvertFrom("pack://application:,,,/Sunny.Controls;component/Toolbar/Images/SimpleSearch.png");
					this._SimpleSearchButton.Text = "查询";
					this._SimpleSearchButton.Margin = new Thickness(5, 0, 0, 0);
					this._SimpleSearchButton.Click += SimpleSearchButton_Click;
				}
				return this._SimpleSearchButton;
			}
		}

		private void SimpleSearchButton_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseQueryEvent(new QueryEventArgs(this.Searcher.WhereCommand, null));
		}

		private Sunny.Controls.Button _AdvanceSearchButton;
		public Sunny.Controls.Button AdvanceSearchButton
		{
			get
			{
				if (this._AdvanceSearchButton == null)
				{
					this._AdvanceSearchButton = new Sunny.Controls.Button();
					this._AdvanceSearchButton.Icon = (System.Windows.Media.ImageSource)new System.Windows.Media.ImageSourceConverter().ConvertFrom("pack://application:,,,/Sunny.Controls;component/Toolbar/Images/AdvanceSearch.png");
					this._AdvanceSearchButton.Text = "高级查询";
					this._AdvanceSearchButton.Margin = new Thickness(5, 0, 0, 0);
					this._AdvanceSearchButton.Click += AdvanceSearchButton_Click;
				}
				return this._AdvanceSearchButton;
			}
		}

		private void AdvanceSearchButton_Click(object sender, RoutedEventArgs e)
		{
			if (new Window { Title= "高级查询", Content = this.SearcherWindow }.ShowDialog() == true)
				this.RaiseQueryEvent(new QueryEventArgs(this.SearcherWindow.WhereCommand, null));
		}

		private Sunny.Controls.Button _ClearButton;
		public Sunny.Controls.Button ClearButton
		{
			get
			{
				if (this._ClearButton == null)
				{
					this._ClearButton = new Sunny.Controls.Button();
					this._ClearButton.Icon = (System.Windows.Media.ImageSource)new System.Windows.Media.ImageSourceConverter().ConvertFrom("pack://application:,,,/Sunny.Controls;component/Toolbar/Images/Clear.png");
					this._ClearButton.Text = "清除";
					this._ClearButton.Margin = new Thickness(5, 0, 0, 0);
					this._ClearButton.Visibility = Visibility.Collapsed;
					this._ClearButton.Click += ClearButton_Click;
				}
				return this._ClearButton;
			}
		}

		private void ClearButton_Click(object sender, RoutedEventArgs e)
		{
			this.OnClear();
		}

		private Sunny.Controls.Button _ReloadButton;
		public Sunny.Controls.Button ReloadButton
		{
			get
			{
				if (this._ReloadButton == null)
				{
					this._ReloadButton = new Sunny.Controls.Button();
					this._ReloadButton.Icon = (System.Windows.Media.ImageSource)new System.Windows.Media.ImageSourceConverter().ConvertFrom("pack://application:,,,/Sunny.Controls;component/Toolbar/Images/Reload.png");
					this._ReloadButton.Text = "刷新";
					this._ReloadButton.Margin = new Thickness(5, 0, 0, 0);
					this._ReloadButton.Visibility = Visibility.Collapsed;
					this._ReloadButton.Click += ReloadButton_Click;
				}
				return this._ReloadButton;
			}
		}

		private void ReloadButton_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseReloadEvent();
		}

		public bool ShowReloadButton
		{
			get
			{
				return this.ReloadButton.Visibility == Visibility.Visible;
			}
			set
			{
				this.ReloadButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		private Sunny.Controls.Button _ExportButton;
		public Sunny.Controls.Button ExportButton
		{
			get
			{
				if (this._ExportButton == null)
				{
					this._ExportButton = new Sunny.Controls.Button();
					this._ExportButton.Icon = (System.Windows.Media.ImageSource)new System.Windows.Media.ImageSourceConverter().ConvertFrom("pack://application:,,,/Sunny.Controls;component/Toolbar/Images/Export.png");
					this._ExportButton.Text = "导出";
					this._ExportButton.Margin = new Thickness(5, 0, 0, 0);
					this._ExportButton.Visibility = Visibility.Collapsed;
					this._ExportButton.Click += ExportButton_Click;
				}
				return this._ExportButton;
			}
		}

		private void ExportButton_Click(object sender, RoutedEventArgs e)
		{
			this.RaiseExportEvent();
		}

		public bool ShowExportButton
		{
			get
			{
				return this.ExportButton.Visibility == Visibility.Visible;
			}
			set
			{
				this.ExportButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
			}
		}

		private SearcherWindow _SearcherWindow;
		protected SearcherWindow SearcherWindow
		{
			get
			{
				if (this._SearcherWindow == null)
					this._SearcherWindow = new SearcherWindow(this.Searcher);
				return this._SearcherWindow;
			}
		}


		static Toolbar()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(Toolbar), new FrameworkPropertyMetadata(typeof(Toolbar)));
		}

		public Toolbar()
		{
			InitializeComponent();

			this.Buttons.Add(this.SimpleSearchButton);
			this.Buttons.Add(this.AdvanceSearchButton);
			this.Buttons.Add(this.ClearButton);
			this.Buttons.Add(this.ReloadButton);
			this.Buttons.Add(this.ExportButton);
		}

		protected override void OnRender(DrawingContext dc)
		{
			this.SimpleSearchButton.Visibility = this.Searcher.Any(i => i.ShowInSimpleSearcher) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			this.AdvanceSearchButton.Visibility = this.Searcher.Any(i => i.ShowInAdvanceSearcher) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			this.ClearButton.Visibility = this.SimpleSearchButton.Visibility == Visibility.Visible || this.AdvanceSearchButton.Visibility == Visibility.Visible ? Visibility.Visible : Visibility.Collapsed;

			base.OnRender(dc);
		}

		//protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
		//{
		//	base.OnVisualChildrenChanged(visualAdded, visualRemoved);
		//	if (this._Items != null)
		//		this._Items.Refresh();
		//	if (this._Searcher != null)
		//		this._Searcher.Refresh();
		//}

		//protected override void OnRender(DrawingContext dc)
		//{
		//	//if(this.Items.OffsetWidth+this.Searcher.OffsetWidth+this.ButtonContainer.GetOffsetWidth()<this.ActualWidth)
		//	//{
		//	//	this.SearchItemsSpacer.Width = this.ActualWidth - this.Items.OffsetWidth - this.Searcher.OffsetWidth - this.ButtonContainer.GetOffsetWidth();
		//	//	this.ButtonsSpacer.Width = 0;
		//	//}
		//	this.SearchItemsSpacer.Width = 0;
		//	this.ButtonsSpacer.Width = 0;

		//	base.OnRender(dc);
		//}

		private void Toolbar_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (this.ItemContainer.ActualWidth + this.SearcherContainer.ActualWidth + this.ButtonContainer.ActualWidth < this.ActualWidth)
			{
				Grid.SetRow(this.SearcherContainer, 0);
				Grid.SetRow(this.ButtonContainer, 0);
				this.SearcherContainer.Margin = new Thickness(0, 0, this.ButtonContainer.ActualWidth, 0);
				this.ButtonContainer.Margin = new Thickness();
				return;
			}
			if (this.SearcherContainer.ActualWidth + this.ButtonContainer.ActualWidth < this.ActualWidth)
			{
				Grid.SetRow(this.SearcherContainer, 1);
				Grid.SetRow(this.ButtonContainer, 1);
				this.SearcherContainer.Margin = new Thickness(0, 5, this.ButtonContainer.ActualWidth, 0);
				this.ButtonContainer.Margin = new Thickness(0, 5, 0, 0);
				return;
			}
			Grid.SetRow(this.SearcherContainer, 1);
			Grid.SetRow(this.ButtonContainer, 2);
			this.SearcherContainer.Margin = new Thickness(0,5,0,0);
			this.ButtonContainer.Margin = new Thickness(0, 5, 0, 0);
		}
	}
}